package com.hnd.springframework.factory.support;

import com.hnd.springframework.BeansException;
import com.hnd.springframework.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SimpleInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor cots, Object... args) throws BeansException {
        Class clazz = beanDefinition.getBeanClass();
        try {
            if (cots == null) {
                return clazz.newInstance();
            }
            Object instance = clazz.getDeclaredConstructor(cots.getParameterTypes()).newInstance(args);
            return instance;
        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            throw new BeansException("实例化失败！！！！");
        }
    }
}
